package ar.uba.fi.taller2.cmmiassistant.frontend.renderers;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.EnumMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.tree.TreeCellRenderer;

import ar.uba.fi.taller2.cmmiassistant.frontend.adaptadores.arbol.NodoArbol;
import ar.uba.fi.taller2.cmmiassistant.frontend.adaptadores.arbol.TipoNodoArbol;

public class CmmiAssistantTreeCellRenderer extends Renderer implements TreeCellRenderer {

    public static final Map<TipoNodoArbol, Icon> icons;
    
	private static final Font font;

	private static final Color colorSelectedCell = new Color(0.0f, 0.0f, 0.0f,
			0.1f);

	private static final Border borderCell = BorderFactory.createEmptyBorder(2,
			2, 2, 10);

	private static final Border borderSelectedCell = BorderFactory
			.createCompoundBorder(BorderFactory
					.createLineBorder(colorSelectedCell), BorderFactory
					.createEmptyBorder(1, 1, 1, 9));

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		NodoArbol nodo = (NodoArbol) value;
		
		JLabel label = new JLabel();
		label.setText(nodo.getEtiqueta().getNombre());
		label.setIcon(icons.get(nodo.getTipo()));
		label.setFont(font);

		if (selected) {

			label.setBackground(colorSelectedCell);
			label.setOpaque(true);
			label.setBorder(borderSelectedCell);

		} else {

			label.setBorder(borderCell);
		}
		return label;
	}

	static {

		font = new Font("Verdana,Arial", Font.PLAIN, 12);

		
		icons = new EnumMap<TipoNodoArbol, Icon>(TipoNodoArbol.class);
		
		for (TipoNodoArbol tipo : TipoNodoArbol.values()) {
			
			icons.put(tipo, createImageIcon(tipo.getImagen()));
		}
	}
}
